Skip to content

Array

Array Creation and Access

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

Represent collections of related primitive or object reference data using one- dimensional (1D) array objects.

ESSENTIAL KNOWLEDGE

  • The use of array objects allows multiple related items to be represented using a single variable.

  • The size of an array is established at the time of creation and cannot be changed.

  • Arrays can store either primitive data or object reference data.

  • When an array is created using the keyword new, all of its elements are initialized with a specific value based on the type of elements:

    • Elements of type int are initialized to 0
    • Elements of type double are initialized to 0.0
    • Elements of type boolean are initialized
    • Elements of a reference type are initialized to the reference value null. No objects are automatically created
  • Initializer lists can be used to create and initialize arrays.

  • Square brackets ([ ]) are used to access and modify an element in a 1D array using an index.

  • The valid index values for an array are 0 through one less than the number of elements in the array, inclusive. Using an index value outside of this range will result in an ArrayIndexOutOfBoundsException being thrown.

Traversing Arrays

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

Traverse the elements in a 1D array.

ESSENTIAL KNOWLEDGE

  • Iteration statements can be used to access all the elements in an array. This is called traversing the array.

  • Traversing an array with an indexed for loop or while loop requires elements to be accessed using their indices.

  • Since the indices for an array start at 0 and end at the number of elements −1, “off by one” errors are easy to make when traversing an array, resulting in an ArrayIndexOutOfBoundsException being thrown.

EnhancedforLoop for Arrays

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

Traverse the elements in a 1D array object using an enhanced for loop.

ESSENTIAL KNOWLEDGE

  • An enhanced for loop header includes a variable, referred to as the enhanced for loop variable.

  • For each iteration of the enhanced for loop, the enhanced for loop variable is assigned a copy of an element without using its index.

  • Assigning a new value to the enhanced for loop variable does not change the value stored in the array.

  • Program code written using an enhanced for loop to traverse and access elements in an array can be rewritten using an indexed for loop or a while loop.

Developing Algorithms Using Arrays

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

For algorithms in the context of a particular specification that requires the use of array traversals:

  • Identify standard algorithms.
  • Modify standard algorithms.
  • Develop an algorithm.

ESSENTIAL KNOWLEDGE

  • There are standard algorithms that utilize array traversals to:

    • Determine a minimum or maximum value
    • Compute a sum, average, or mode
    • Determine if at least one element has a particular property
    • Determine if all elements have a particular property
    • Access all consecutive pairs of elements
    • Determine the presence or absence of duplicate elements
    • Determine the number of elements meeting specific criteria
  • There are standard array algorithms that utilize traversals to: § Shift or rotate elements left or right § Reverse the order of the elements